package ck.android.englishschool;

import ck.android.englishschool.home.HomeActivity;
import ck.android.englishschool.home.SentenceFragment;
import ck.android.englishschool.home.WordFragment;
import ck.android.englishschool.home.WordInputFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class MenuControl extends ListFragment implements OnItemClickListener
{
	
	static HomeActivity am;
	public static BaseFragment tagetFragment = null;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		am = (HomeActivity) HomeActivity.am;
		
		MenuAdapter adapter = new MenuAdapter(getActivity());
		
		MenuItem wordMenu = new MenuItem(R.drawable.menu01_selector);
		MenuItem sentenceMenu = new MenuItem(R.drawable.menu02_selector);
		MenuItem wordInputMenu = new MenuItem(R.drawable.menu03_selector);
		
		adapter.add(wordMenu);
		adapter.add(sentenceMenu);
		adapter.add(wordInputMenu);
		
		setListAdapter(adapter);
		
		ListView lv = getListView();
		lv.setOnItemClickListener(this);
		lv.setDividerHeight(0);
		lv.setDivider(null);
	}

	private class MenuItem
	{
		public String tag;
		public int iconRes;

	/*	public MenuItem(String tag, int iconRes) {
			this.tag = tag;
			this.iconRes = iconRes;
		}*/
		public MenuItem( int iconRes) {
			this.iconRes = iconRes;
		}
	}

	public class MenuAdapter extends ArrayAdapter<MenuItem>
	{

		public MenuAdapter(Context context) {
			super(context, 0);
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			//icon.setImageResource(getItem(position).iconRes);
			icon.setBackgroundResource(getItem(position).iconRes);
			/*TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).tag);*/

			return convertView;
		}

	}
	
	
	public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
		Intent it;
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		
		switch (pos) {
		//Word 메뉴
		case 0:
//			clearBackStack();
			if(tagetFragment != null){
				ft.remove(tagetFragment);
				tagetFragment = null;
			}
			ft.commit();		
			am.toggle();
			
			WordFragment wordFragment = new WordFragment();
			addFragmentReplace(R.id.base_frame, wordFragment, WordFragment.class.getName());
			
			break;
			
		//Sentences 메뉴
		case 1:
			if(tagetFragment != null){
				ft.remove(tagetFragment);
				tagetFragment = null;
			}
			ft.commit();		
			am.toggle();
			
			SentenceFragment sentenceFragment = new SentenceFragment();
			addFragmentReplace(R.id.base_frame, sentenceFragment, SentenceFragment.class.getName());
			
			break;
			
		//WordInput 메뉴
		case 2:
			if(tagetFragment != null){
				ft.remove(tagetFragment);
				tagetFragment = null;
			}
			ft.commit();		
			am.toggle();
			
			WordInputFragment wordInputFragment = new WordInputFragment();
			addFragmentReplace(R.id.base_frame, wordInputFragment, WordInputFragment.class.getName());
			
			break;
		}
	}
	
	
	public void clearBackStack(){
		FragmentManager fragmentManager = getFragmentManager();
		fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	}
	
	public void addFragmentReplace(int layoutId, BaseFragment baseFragment, String name) {
		tagetFragment = baseFragment;
		//clearBackStack();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.replace(layoutId, baseFragment, name);
		ft.commit();			
	}	
}
